<?php
/**
 * 初始化env文件
 * @author yupoxiong<i@yupoxiong.com>
 */

declare (strict_types=1);


namespace app\command;

use app\common\service\EnvService;
use Symfony\Component\Console\Command\Command;
use app\common\exception\CommonServiceException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class InitEnv extends Command
{
    protected static $defaultName = 'init:env';
    protected static $defaultDescription = '初始化env文件';

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $output->writeln('即将初始化env文件：');

        $force = $input->getOption('force');

        $service = new EnvService();
        try {

            $env = base_path() . '/.env';
            if (!file_exists($env)) {
                $service->createEnv($force);
                $service->refreshApiJwtKey();
            }else{
                $service->createEnv($force);
            }

            $output->writeln('env文件已初始化');
        } catch (CommonServiceException $e) {
            $output->writeln('<error>' . $e->getMessage() . '</error>');
        }

        return self::SUCCESS;
    }

    /**
     * @return void
     */
    protected function configure()
    {
        // 是否强制生成文件
        $this->addOption('force', 'f', null, '是否强制生成文件');
    }
}